/// Creator:      fengliang
/// CreatDate:    2020-07-17
/// Description:  输液医嘱
Class Nur.MNISV3.Service.InfusionOrd Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// Creator:      fengliang
/// CreatDate:    2021-04-13
/// Description:  静配接收
/// Input：       oeoreId:执行记录ID,userId:用户ID,locId:科室Id
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).OrdRec("568-3-1","1",1)     
ClassMethod OrdRec(oeoreId As %String, userId As %String, locId As %String) As %String
{
    n (oeoreId, userId, locId)
    s $zt="errOrdRec"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s oeoreId=$replace(oeoreId,"-","||")

    s workType="12"
    s scErr=##class(Nur.MNISV3.IPIFWorkLoad).Save("","", userId , locId, oeoreId, workType)  //插入工作量表
    i scErr["err" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)        

    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errOrdRec
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      fengliang
/// CreatDate:    2021-04-13
/// Description:  摆药
/// Input：       oeoreId:执行记录ID,userId:用户ID,locId:科室Id
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).PutOrd("568-3-1","1",1)     
ClassMethod PutOrd(oeoreId As %String, userId As %String, locId As %String) As %String
{
    n (oeoreId, userId, locId)
    s $zt="errPutOrd"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s oeoreId=$replace(oeoreId,"-","||")

    s workType="11"
    s scErr=##class(Nur.MNISV3.IPIFWorkLoad).Save("","", userId , locId, oeoreId, workType)  //插入工作量表
    i scErr["err" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)        

    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errPutOrd
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      lmm
/// CreatDate:    2019-11-26
/// Description:  配液/复核
/// Input：       oeoreId:执行记录ID,userId:用户ID,type:操作类型(Despensing,Audit),locId:科室Id
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).despensingOrd("568-3-1","1","Despensing",1)     
ClassMethod DespensingOrd(oeoreId As %String, userId As %String, type As %String, locId As %String, userCode As %String = "", password As %String = "") As %String
{
    n (oeoreId, userId, type, locId,userCode,password)
    s $zt="errDespensingOrd"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s oeoreId=$replace(oeoreId,"-","||")

    s workType="1"
    i type="Despensing" s workType=1  //配液
    i type="Audit" s workType=2  //复核
    s scErr=##class(Nur.MNISV3.IPIFWorkLoad).Save("","", userId , locId, oeoreId, workType)  //插入工作量表
    i scErr["err" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)        

    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errDespensingOrd
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      lmm
/// CreatDate:    2019-11-26
/// Description:  执行(穿刺/续液)
/// Input：       oeoreId:执行记录ID,userId:用户ID,locId:科室Id,distantTime:预计完成时间,ifSpeed:滴速,puncturePart:穿刺部位
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).ExeOrd("178||231||1","12178","114","","44 滴/秒","","0000000134658501")     
ClassMethod ExeOrd(oeoreId As %String, userId As %String, locId As %String, distantTime As %String = "", ifSpeed As %String = "", puncturePart As %String = "", wayNo = "") As %String
{
    n (oeoreId, userId, locId,distantTime,ifSpeed,puncturePart,wayNo)
    s $zt="errExeOrd"
    ;s ^MML("ExeOrd")=$lb(oeoreId, userId, locId,distantTime,ifSpeed,puncturePart,wayNo)
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s oeoreId=$replace(oeoreId,"-","||")
    s lastOrdId=##class(Nur.MNIS.IPIFTimekeeping).GetLastOrdId(oeoreId,wayNo)
    
    ts
    //修改上一组液体的输液计时
    s parr="InfusionFinishFlag|"_"Y"_"^InfusionFinishDate|"_+$h_"^InfusionFinishTime|"_$p($h,",",2)_"^InfusionFinishUser|"_userId
    s scErr=##class(Nur.MNIS.IPIFTimekeeping).SaveLast(parr,oeoreId, userId , locId,wayNo)
    i scErr["没有输液中的液体" s workType=3 //穿刺
    e  s workType=4  //续液
    i scErr["err"&&(scErr'["没有输液中的液体") d errPTro tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
    
    //插入工作量
    s parr="WorkInfusionSpeed|"_ifSpeed_"^WorkPuncturePart|"_puncturePart
    s scErr=##class(Nur.MNISV3.IPIFWorkLoad).Save(parr,"", userId , locId, oeoreId, workType)
    i scErr["err" d errPTro tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
            
    //插入输液计时 
    //解析预计完成时间
    s InfusionPreFinishDate="",InfusionPreFinishTime=""
    i distantTime'=""  d  
    .i distantTime[" "  d
    ..s InfusionPreFinishDate=$p(distantTime," ",1)
    ..i InfusionPreFinishDate'="" s InfusionPreFinishDate=$zdh(InfusionPreFinishDate,3)
    ..s InfusionPreFinishTime=$p(distantTime," ",2)
    ..i InfusionPreFinishTime'="" s InfusionPreFinishTime=$zth(InfusionPreFinishTime)
    .e  d
    ..s InfusionPreFinishDate=+$h
    ..s InfusionPreFinishTime=$zth(distantTime)
    s parr="InfusionSpeed|"_ifSpeed_"^InfusionPreFinishDate|"_InfusionPreFinishDate_"^InfusionPreFinishTime|"_InfusionPreFinishTime_"^InfusionWayNo|"_wayNo
    s scErr=##class(Nur.MNIS.IPIFTimekeeping).Save(parr,oeoreId, userId , locId)
    i scErr["err" d errPTro tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
    
    s trait=##class(%ArrayOfDataTypes).%New() // 插护理病历
    d trait.SetAt(ifSpeed,"ifSpeed")
    d trait.SetAt(puncturePart,"puncturePart")
    d trait.SetAt(oeoreId,"oeoreId")
    s episodeID=+$g(^OEORD(+oeoreId))
    s operation=$s(workType=3:"Puncture",1:"Change")
    d ##class(Nur.MNIS.Service.InsertEmr).Save(episodeID,operation,"","",userId,trait)
    
    tc
    s oeordID=+oeoreId,oeoriSub=$p(oeoreId,"||",2),oeoreSub=$p(oeoreId,"||",3)
    s execStatusID=$p($g(^OEORD(oeordID,"I",oeoriSub,"X",oeoreSub)),"^",16)
    s execStatusCode=$s(execStatusID'="":$p($g(^OEC("STAT",execStatusID)),"^",1),1:"")
    i (execStatusCode="F") d
    .i (lastOrdId'="") d ##class(Nur.MNIS.Service.EnsInterface).UpdateSystemStatus(lastOrdId,"OrderExecute","finish",userId,locId,"","")
    .d ##class(Nur.MNIS.Service.EnsInterface).UpdateSystemStatus(oeoreId,"OrderExecute","execute",userId,locId,"","")
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errExeOrd
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
errPTro
    s msg=scErr,msgcode="199992",status="-1"
    q
}

/// Creator:      lmm
/// CreatDate:    2019-11-26
/// Description:  巡视
/// Input：       oeoreId:执行记录ID,userId:用户ID,locId:科室Id,distantTime:预计完成时间,ifSpeed:滴速,puncturePart:穿刺部位,tourContent:数据串(key|value^key|value)
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).tourOrd("871282||2||1","7","691","2019-07-12 11:24:00","45","","WorkInfusionState|暂停^WorkInfusionReason|鼓针^WorkInfusionMeasure|他去去去")
ClassMethod TourOrd(oeoreId As %String, userId As %String, locId As %String, distantTime As %String = "", ifSpeed As %String = "", puncturePart As %String = "", tourContent As %String = "") As %String
{
    n (oeoreId, userId, locId,distantTime,ifSpeed,puncturePart,tourContent)
    s $zt="errTourOrd"
    ;s ^EH("TourOrd")=$lb(oeoreId,userId,locId,distantTime,ifSpeed,puncturePart,tourContent)
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s oeoreId=$replace(oeoreId,"-","||")
    
    //解析巡视内容串
    s tmp=""
    d ##class(web.DHCMGNurComm).setmoudtmp(tourContent,.tmp)
    i $d(tmp("WorkInfusionState")) s infusionState=tmp("WorkInfusionState")
    i $d(tmp("WorkInfusionMeasure")) s infusionMeasure=tmp("WorkInfusionMeasure")
    i $d(tmp("WorkInfusionReason")) s infusionReason=tmp("WorkInfusionReason")  
    

    //插入工作量
    /*s workType=5  //巡视
    s parr="WorkInfusionSpeed|"_ifSpeed_"^WorkPuncturePart|"_puncturePart_"^WorkInfusionState|"_$g(infusionState)
    s parr=parr_"^WorkInfusionReason|"_$g(infusionReason)_"^WorkInfusionMeasure|"_$g(infusionMeasure)

    s scErr=##class(Nur.MNISV3.IPIFWorkLoad).Save(parr,"", userId , locId, oeoreId, workType)
    i scErr["err"  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)*/
        
        
    s parr="DHCNurTourAdmDR|"_+^OEORD(+oeoreId)_"^DHCNurTourCtlocDR|"_locId_"^DHCNurTourPatBed|^DHCNurTourCareDR|^DHCNurTourCareDesc|^DHCNurTourUser|"_userId_"^DHCNurTourType|Infusion^DHCNurTourInspectDR|"
    s parr=parr_$replace(oeoreId,"||","-")_"^DHCNurTourDate|"_$zd(+$h,3)_"^DHCNurTourTime|"_$zt($p($h,",",2))_"^DHCNurTourInspectSP|"_ifSpeed_"^DHCNurTourInspectSTAT|"_$g(infusionReason)
    s ret=##class(Nur.MNIS.Service.TourRec).SaveTour(parr, "","Infusion",userId)
        
        
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errTourOrd
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      lmm
/// CreatDate:    2019-11-26
/// Description:  暂停
/// Input：       oeoreId:执行记录ID,userId:用户ID,locId:科室Id,distantTime:预计完成时间,ifSpeed:滴速,puncturePart:穿刺部位,tourContent:数据串(key|value^key|value)
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).tourOrd("609-3-4","10211","249","2019-07-12 11:24:00","45","","WorkInfusionState|暂停^WorkInfusionReason|鼓针^WorkInfusionMeasure|他去去去")
ClassMethod SuspendOrd(oeoreId As %String, userId As %String, locId As %String, infusionReason As %String = "", infusionState As %String = "", ifSpeed = "") As %String
{
    n (oeoreId, userId, locId,infusionReason,infusionState, ifSpeed)
    ;s ^wmq("suspendOrd")=$lb(oeoreId, userId, locId,infusionReason,infusionState, ifSpeed)
    s $zt="errSuspend"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s oeoreId=$replace(oeoreId,"-","||")    

    //插入工作量
    s workType=6  //暂停
    s parr="WorkInfusionReason|"_$g(infusionReason)_"^WorkInfusionState|"_$g(infusionState)

    s scErr=##class(Nur.MNISV3.IPIFWorkLoad).Save(parr,"", userId , locId, oeoreId, workType)
    i scErr["err"  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
        
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errSuspend
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      lmm
/// CreatDate:    2019-11-26
/// Description:  继续执行
/// Input：       oeoreId:执行记录ID,userId:用户ID,locId:科室Id,distantTime:预计完成时间,ifSpeed:滴速,puncturePart:穿刺部位,tourContent:数据串(key|value^key|value)
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).tourOrd("609-3-4","10211","249","2019-07-12 11:24:00","45","","WorkInfusionState|暂停^WorkInfusionReason|鼓针^WorkInfusionMeasure|他去去去")
ClassMethod ContinueOrd(oeoreId As %String, userId As %String, locId As %String, infusionReason As %String = "", infusionState As %String = "", ifSpeed = "") As %String
{
    n (oeoreId, userId, locId,infusionReason,infusionState, ifSpeed)
    s $zt="errContinueOrd"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s oeoreId=$replace(oeoreId,"-","||")    

    //插入工作量
    s workType=7  //继续执行
    s parr="WorkInfusionReason|"_$g(infusionReason)_"^WorkInfusionState|"_$g(infusionState)

    s scErr=##class(Nur.MNISV3.IPIFWorkLoad).Save(parr,"", userId , locId, oeoreId, workType)
    i scErr["err"  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
        
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errContinueOrd
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      lmm
/// CreatDate:    2019-11-26
/// Description:  停止
/// Input：       oeoreId:执行记录ID,userId:用户ID,locId:科室Id,distantTime:预计完成时间,ifSpeed:滴速,puncturePart:穿刺部位,tourContent:数据串(key|value^key|value)
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).tourOrd("609-3-4","10211","249","2019-07-12 11:24:00","45","","WorkInfusionState|暂停^WorkInfusionReason|鼓针^WorkInfusionMeasure|他去去去")
ClassMethod StopOrd(oeoreId As %String, userId As %String, locId As %String, infusionReason As %String = "", infusionState As %String = "", ResidualQty As %String = "", ifSpeed = "") As %String
{
    n (oeoreId, userId, locId,infusionReason,infusionState,ResidualQty, ifSpeed)
    s $zt="errStopOrd"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s oeoreId=$replace(oeoreId,"-","||")    
    ts
    //插入工作量
    s workType=8  //停止执行
    s parr="WorkInfusionReason|"_$g(infusionReason)_"^WorkInfusionState|"_$g(infusionState)_"^WorkResidualQty|"_$g(ResidualQty)

    s scErr=##class(Nur.MNISV3.IPIFWorkLoad).Save(parr,"", userId , locId, oeoreId, workType)
    i scErr["err" d errTro tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
    
    //输液计时表置完成 
    s parr="InfusionFinishFlag|"_"N"_"^InfusionFinishDate|"_+$h_"^InfusionFinishTime|"_$p($h,",",2)_"^InfusionFinishUser|"_userId
    s scErr=##class(Nur.MNIS.IPIFTimekeeping).Save(parr,oeoreId, userId , locId)
    i scErr["err" d errTro tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
    tc
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errStopOrd
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      lmm
/// CreatDate:    2019-11-26
/// Description:  拔针
/// Input：       oeoreId:执行记录ID,userId:用户ID,locId:科室Id,distantTime:预计完成时间,ifSpeed:滴速,puncturePart:穿刺部位,tourContent:数据串(key|value^key|value)
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).tourOrd("609-3-4","10211","249","2019-07-12 11:24:00","45","","WorkInfusionState|暂停^WorkInfusionReason|鼓针^WorkInfusionMeasure|他去去去")
ClassMethod FinishOrd(oeoreId As %String, userId As %String, locId As %String, ifSpeed = "") As %String
{
    n (oeoreId, userId, locId, ifSpee)
    s $zt="errFinishOrd"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s oeoreId=$replace(oeoreId,"-","||")    
    ts  
    //插入工作量
    s workType=9  //停止执行
    s parr="WorkInfusionReason|"_$g(infusionReason)_"^WorkInfusionState|"_$g(infusionState)

    s scErr=##class(Nur.MNISV3.IPIFWorkLoad).Save(parr,"", userId , locId, oeoreId, workType)
    i scErr["err" d errTro tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
    
    //输液计时表置完成 
    s parr="InfusionFinishFlag|"_"Y"_"^InfusionFinishDate|"_+$h_"^InfusionFinishTime|"_$p($h,",",2)_"^InfusionFinishUser|"_userId
    s scErr=##class(Nur.MNIS.IPIFTimekeeping).Save(parr,oeoreId, userId , locId)
    i scErr["err" d errTro tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
        
    tc
    d ##class(Nur.MNIS.Service.EnsInterface).UpdateSystemStatus(oeoreId,"OrderExecute","finish",userId,locId,"","")
        
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)    
errFinishOrd
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
errTro
    s msg=scErr,msgcode="199992",status="-1"
    q
}

/// Creator:      fengliang
/// CreatDate:    2021-04-14
/// Description:  获取输液状态
/// other:        w ##class(Nur.MNISV3.Service.InfusionOrd).GetInfusionOrdState("178||229||1")
ClassMethod GetInfusionOrdState(oeoreId As %String) As %String
{
    n (oeoreId)
    s oeoreId=$replace(oeoreId,"-","||")
    s wordType=""
    s WorkDate=""
    f
    {
        s WorkDate=$o(^Nur.MNISV3.IPIFWorkLoadI("OeoreDateTime"," "_oeoreId,WorkDate),-1)
        q:WorkDate=""
        s WorkTime=""
        f
        {
          s WorkTime=$o(^Nur.MNISV3.IPIFWorkLoadI("OeoreDateTime"," "_oeoreId,WorkDate,WorkTime),-1)
          q:WorkTime=""
          s WorkId=""
          f 
          {
              s WorkId=$o(^Nur.MNISV3.IPIFWorkLoadI("OeoreDateTime"," "_oeoreId,WorkDate,WorkTime,WorkId),-1)
              q:WorkId=""
              q:wordType'=""
              s rec=##class(Nur.MNISV3.IPIFWorkLoad).%OpenId(WorkId)
              b ;bb3
              s wordType=rec.WorkType.WorkTypeCode
              b ;bb4
              d rec.%Close()
          }
        }
    }
    q wordType
}

/// Creator:      fengliang
/// CreatDate:    2020-10-23
/// Description:  获取配液信息
ClassMethod GetPeiYaoInfo(oeoreId) As %String
{
    n (oeoreId)
    s dhcoreId=$o(^DHCOrdExec(0,"OEOREDR",oeoreId,""))
    s ret=""
    if dhcoreId'=""
    {
      if $D(^DHCOrdExec(dhcoreId))
      {
          s flag=$P(^DHCOrdExec(dhcoreId),"^",22)
          i flag="" s flag="N"
          s PeiYaodate=$P(^DHCOrdExec(dhcoreId),"^",24)
          s PeiYaotime=$P(^DHCOrdExec(dhcoreId),"^",25)
          s userId=$P(^DHCOrdExec(dhcoreId),"^",23)
          s PeiYaouser=userId
          s ret=flag_"^"_PeiYaodate_"^"_PeiYaotime_"^"_PeiYaouser
      }
    }
    q ret
}

/// 输液总览查询
/// w ##class(Nur.MNISV3.Service.InfusionOrd).GetInfusionDataV4("","3","20665","8","2025-08-06","2025-08-06","{""single"":"""",""multiple"":""1""}")
ClassMethod GetInfusionDataV4(episodeId, wardId, userId, locId, fromDate, toDate, extras)
{
    n (episodeId,wardId, userId, locId,fromDate,toDate,extras)
    s $zt="errGetInfusionDataV4"
    s ^GRS("GetInfusionDataV4")=$lb(episodeId,wardId, userId, locId,fromDate,toDate,extras)
    s msg="成功",msgcode="999999",status="0" //定义初始值
    i fromDate'="" s fromDate=##class(websys.Conversions).DateHtmlToLogical(fromDate)
    i toDate'="" s toDate=##class(websys.Conversions).DateHtmlToLogical(toDate)
    s patInfoList=##class(%ListOfDataTypes).%New()
    i episodeId'="" {//单患者
        s patinfo=##class(%ArrayOfDataTypes).%New()
        s regNo=##class(Nur.NIS.Service.Base.Patient).GetRegNo(episodeId)
        d getCurInfusionInfoV4
        s deviceNo=$G(^DHCDviceNo("SYNO","RegNo",regNo))
        d patinfo.SetAt($g(infusionData),"infusionData")
        d patinfo.SetAt($g(infusionStatistic),"infusionStatistic")
        d patinfo.SetAt(deviceNo,"deviceNo")
        //基本患者信息
        s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
        d patInfoList.Insert(patinfo)
    }else {  
        //调用纯净版患者列表，
        s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
        d patListObj.Execute(wardId,userId,"Ward",locId)
        while(patListObj.Next()){
            s patinfo=##class(Nur.MNISV3.Common.Utils).ResultSet(patListObj)
            s episodeId=patListObj.GetDataByName("episodeId")
            s regNo=##class(Nur.NIS.Service.Base.Patient).GetRegNo(episodeId)
           
            d getCurInfusionInfoV4
            s deviceNo=$G(^DHCDviceNo("SYNO","RegNo",regNo))
            d patinfo.SetAt($g(infusionData),"infusionData")
            s infusionStatistic=""
            i $g(wayNum)>0 s infusionStatistic="共 "_$g(wayNum)_" 组"
            d patinfo.SetAt(infusionStatistic,"infusionStatistic") //表示数量：输液中/总数）
            d patinfo.SetAt(deviceNo,"deviceNo")
            //基本患者信息
            s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
            d patInfoList.Insert(patinfo)
        }
    }

    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(patInfoList),"patients")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)   
errGetInfusionDataV4
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
getCurInfusionInfoV4
    s startDateTime="",finishDateTime="",overTime="",preOverTime="",oeoreId="",infusionStatusColor="" 
    s infusionData=##class(%ListOfDataTypes).%New()
    s wayNum=0
    i toDate="" s toDate=+$h
    i fromDate="" s fromDate=+$h
    f date=fromDate:1:toDate
    {
        s id=""
        s groupOrdList=""
        f
        {
            s id=$o(^Nur.MNIS.IPIFTimekeepingI("RegNoDate"," "_regNo,date,id))
            q:id=""
            s rec=##class(Nur.MNIS.IPIFTimekeeping).%OpenId(id)
            s startDate=rec.InfusionStartDate
            s startTime=rec.InfusionStartTime
            s finishDate=rec.InfusionFinishDate
            s finishTime=rec.InfusionFinishTime
            s preFinishDate=rec.InfusionPreFinishDate
            s preFinishTime=rec.InfusionPreFinishTime
            s oeoreId=rec.InfusionOeoreDr
            s wayNo=rec.InfusionWayNo
            d rec.%Close()
            i preFinishDate'="" s preFinishDateTime=$zd(preFinishDate,3)_" "_$zt(preFinishTime,2)
            s startDateTime=$zd(startDate,3)_" "_$zt(startTime,2)
            i finishDate'="" s finishDateTime=$zd(finishDate,3)_" "_$zt(finishTime,2)
            e  s finishDateTime=""
            
            s overTime=(finishDate*86400)+finishTime-(startDate*86400)-startTime
            i overTime>86400 s overTime="大于一天"
            e  d
            .i overTime<0 s overTime=0
            .e  s overTime=$zt(overTime,2)
            
            s infusionStatus=$s($g(finishDateTime)="":"输液中",1:"完成")
            s preOverTime=""
            i preFinishDate'="" d
            .s preOverTime=(preFinishDate*86400)+preFinishTime-(startDate*86400)-startTime
            .i preOverTime<(5*60) s infusionStatus="即将完成"
            .i preOverTime>86400 s preOverTime="大于一天"
            .e  d
            ..i preOverTime<0 s preOverTime=0
            ..e  s preOverTime=$zt(preOverTime,2)

            s infusionStatusColor=$s($g(infusionStatus)="即将完成":"#FF0000",$g(infusionStatus)="完成":"#ADADAD",1:"#1AFD9C") 
        
            s wayNoOrdData=##class(%ArrayOfDataTypes).%New()
            d initExecInfoV4
            s wayNum=wayNum+1
            d wayNoOrdData.SetAt(wayNum_"通道","wayNo")
            d wayNoOrdData.SetAt($g(startDateTime),"startDateTime")
            d wayNoOrdData.SetAt($g(finishDateTime),"finishDateTime")
            d wayNoOrdData.SetAt($g(overTime),"overTime")
            d wayNoOrdData.SetAt($g(preOverTime),"preOverTime")
            d wayNoOrdData.SetAt($g(infusionStatus),"infusionStatus")
            d wayNoOrdData.SetAt($g(infusionStatusColor),"infusionStatusColor")
            d wayNoOrdData.SetAt($g(oeoreId),"oeoreId")
            d infusionData.Insert(wayNoOrdData)
       
        }
   }
   q
initExecInfoV4 //初始化执行记录信息
    s oeordID=$p(oeoreId,"||",1)
    s oeoriSub=$p(oeoreId,"||",2)
    s oeoreSub=$p(oeoreId,"||",3)
    s mainOrderInfo=##class(Nur.MNISV3.Service.Order).GetBasicInfo(oeoreId,wayNoOrdData)
    s childs=##class(%ListOfDataTypes).%New()
    //查询子医嘱
    s child=0  
    f 
    {
       
        s mainOeoreID=oeordID_"||"_oeoriSub_"||"_oeoreSub
        s child=$o(^OEORDi(0,"OEORE",mainOeoreID,oeordID,child))
        q:child=""
        s childSub=0  
        f 
        {
            s childSub=$o(^OEORDi(0,"OEORE",mainOeoreID,oeordID,child,childSub))
            q:childSub=""
            s ordType=##class(Nur.MNIS.Service.Order).GetDrugFlag(oeordID,child)
            continue:ordType'="R"
            s orderInfo=##class(Nur.MNISV3.Service.Order).GetBasicInfo(oeordID_"||"_child_"||"_childSub)
            d childs.Insert(orderInfo)
         }
    }
    d mainOrderInfo.SetAt(childs,"children")
    
    q
}

/// Creator:        fengliang
/// CreatDate:      2021-04-14
/// Description:    获取病区患者输液通道列表
/// w ##class(Nur.MNISV3.Service.InfusionOrd).GetWayNoByRegNo("0000000689")
ClassMethod GetWayNoByRegNo(regNo)
{
    n (regNo)
    s $zt="errGetWayNoByRegNo"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    k wayNoSort
    s id=""
    f
    {
        s id=$o(^Nur.MNIS.IPIFTimekeepingI("RegNoDate"," "_regNo,+$h,id),-1)
        q:id=""
        s rec=##class(Nur.MNIS.IPIFTimekeeping).%OpenId(id)
        s startDate=rec.InfusionStartDate
        s startTime=rec.InfusionFinishTime
        s finishDate=rec.InfusionFinishDate
        s finishTime=rec.InfusionFinishTime
        s preFinishDate=rec.InfusionPreFinishDate
        s preFinishTime=rec.InfusionPreFinishTime
        s oeoreId=rec.InfusionOeoreDr
        s wayNo=rec.InfusionWayNo
        d rec.%Close()
        i finishDate=""&&(wayNo'="") s wayNoSort(wayNo)=1   
    }
    s wayNoList=##class(%ListOfDataTypes).%New()
    s wayNo="",num=1
    f
    {
        s wayNo=$o(wayNoSort(wayNo))
        q:wayNo=""  
        s num=num+1
    }
    s wayNoData=##class(%ArrayOfDataTypes).%New()
    d wayNoData.SetAt("新通道","wayNum")
    d wayNoData.SetAt(regNo_+$h_num,"wayNo")
    d wayNoList.Insert(wayNoData)
    s wayNo="",num=1
    f
    {
        s wayNo=$o(wayNoSort(wayNo))
        q:wayNo=""  
        s wayNoData=##class(%ArrayOfDataTypes).%New()
        d wayNoData.SetAt(num_"通道","wayNum")
        d wayNoData.SetAt(wayNo,"wayNo")
        d wayNoList.Insert(wayNoData)
        s num=num+1
        
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    d json.SetAt($g(wayNoList),"wayNoList")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetWayNoByRegNo
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      fengliang
/// CreatDate:    2021-10-12
/// Description:  获取设备列表
/// Others： w ##class(Nur.MNISV3.Service.InfusionOrd).GetDeviceNoByRegNo("0000001627")
ClassMethod GetDeviceNoByRegNo(regNo)
{
    n (regNo)
    s regNoDevice=""
    s http=##class(%Net.HttpRequest).%New()
    s http.Server="81.70.3.211"
    s http.Port=8000
    d http.InsertFormData("adm",regNo)
    d http.Post("/iots/infuse/curstate?")
    ;w http.HttpResponse.Data.Read()
    s returnObj=##class(Nur.MNISV3.Common.JSON).Decode(http.HttpResponse.Data.Read())
    q:returnObj.GetAt("code")'="9999" ""
    s deviceno= returnObj.GetAt("data").GetAt("deviceno")
    s rate= returnObj.GetAt("data").GetAt("rate")
    s remainder= returnObj.GetAt("data").GetAt("remainder")
    q deviceno_"^"_rate_"^"_remainder
}

/// Creator:      fengliang
/// CreatDate:    2021-10-12
/// Description:  绑定设备
/// Others： w ##class(Nur.MNISV3.Service.InfusionOrd).RemoveDeviceNo(10001299)
ClassMethod AddDeviceNo(regNo, deviceNo)
{
    n (regNo,deviceNo)
    s $zt="errAddDeviceNo"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    q:deviceNo="" ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("未找到设备!","1000000","-1")
    q:regNo="" ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("未绑定设备!","1000000","-1")
    s ^DHCDviceNo("SYNO","RegNo",regNo)=deviceNo
    s ^DHCDviceNo("SYNO","DevNo",deviceNo)=regNo
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
    q json
errAddDeviceNo
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      fengliang
/// CreatDate:    2021-10-12
/// Description:  解绑设备
/// Others： w ##class(Nur.MNISV3.Service.InfusionOrd).RemoveDeviceNo(10001299)
ClassMethod RemoveDeviceNo(deviceNo)
{
    n (deviceNo)
    s $zt="errRemoveDeviceNo"
    s msg="成功",msgcode="999999",status="0" //定义初始值
    ;s ^DHCDviceNo("SYNO","RegNo",regNo)=deviceNo
    ;s ^DHCDviceNo("SYNO","DevNo",deviceNo)=regNo
    q:deviceNo="" ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("未找到设备!","1000000","-1")
    s regNo=$g(^DHCDviceNo("SYNO","DevNo",deviceNo))
    q:regNo="" ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("未绑定设备!","1000000","-1")
    k ^DHCDviceNo("SYNO","DevNo",deviceNo)
    k ^DHCDviceNo("SYNO","RegNo",regNo)

    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
    q json
errRemoveDeviceNo
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

/// Creator:      fengliang
/// CreatDate:    2022-10-12
/// Description:  通过医嘱绑定设备
/// w ##class(Nur.MNISV3.Service.InfusionOrd).AddDeviceNoByOrd("1586||24||4","SYNO001")
ClassMethod AddDeviceNoByOrd(oeoreId, deviceNo)
{
    n (oeoreId,deviceNo)
    s $zt="errAddDeviceNoByOrd"
    ;s ^MML("AddDeviceNoByOrd")=$LB(oeoreId,deviceNo)
    s msg="成功",msgcode="999999",status="0" //定义初始值
    s episodeId=+^OEORD(+oeoreId)
    s paitentId=$p(^PAADM(episodeId),"^",1)
    s regNo=$p(^PAPER(paitentId,"PAT",1),"^",2)
    s ^DHCDviceNo("SYNO","RegNo",regNo)=deviceNo
    s ^DHCDviceNo("SYNO","DevNo",deviceNo)=regNo

    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
    q json
errAddDeviceNoByOrd
    s msg=$ze,msgcode="199999",status="-1"
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
}

}
